package cn.jwcat.service.impl;

import cn.jwcat.dealEntity.DealHealth;
import cn.jwcat.dealEntity.UserHealthDto;
import cn.jwcat.entity.User;
import cn.jwcat.entity.UserHealth;
import cn.jwcat.mapper.UserHealthMapper;
import cn.jwcat.service.IUserHealthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户健康信息表 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-05-03
 */
@Service
public class UserHealthServiceImpl extends ServiceImpl<UserHealthMapper, UserHealth> implements IUserHealthService {

	public static MPJLambdaWrapper<UserHealth> bassHealthMapper() {
		MPJLambdaWrapper<UserHealth> wrapper = new MPJLambdaWrapper<>();
		return wrapper.selectAll(UserHealth.class)
			.select(User::getUname)
			.leftJoin(User.class, User::getUserId, UserHealth::getUserId)
			.eq(User::getStatus, true)
			.orderByDesc(UserHealth::getCreateTime);
	}

	@Override
	public List<UserHealthDto> getHealthInfoByStatus() {
		MPJLambdaWrapper<UserHealth> wrapper = bassHealthMapper();
		return this.getBaseMapper().selectJoinList(UserHealthDto.class, wrapper);
	}

	@Override
	public List<UserHealthDto> postQuery(DealHealth dealHealth) {
		String username = dealHealth.getUsername();
		List<String> time = dealHealth.getTime();
		MPJLambdaWrapper<UserHealth> wrapper = bassHealthMapper();
		if (username != null) wrapper.like(User::getUname, username);
		if (time != null) {
			System.out.println(time.get(0));
			System.out.println(time.get(1));
			wrapper.ge(UserHealth::getCreateTime, time.get(0))
				.le(UserHealth::getCreateTime, time.get(1));
		}
		return this.getBaseMapper().selectJoinList(UserHealthDto.class, wrapper);
	}
}
